<?php

namespace App\Http\Controllers\Api;

use App\Events\addLog;
use App\Http\Controllers\Controller;
use App\Http\Requests\SignUpRequest;
use App\Http\Resources\EnrollmentSituationResource;
use App\Http\Resources\PoliceStationResource;
use App\Http\Resources\PrintResource;
use App\Http\Resources\StudentResource;
use App\Http\Resources\StudentUpdateResource;
use App\Models\Entity;
use App\Models\PoliceStation;
use App\Models\PoliceStationHasSchool;
use App\Models\School;
use App\Models\Student;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class StudentController extends Controller
{


    public function index(Request $request)
    {
        $keyword = $request->get('keyword', '');
        $query = Student::query()->where('first_kid',1);
        $models = $query->when($keyword, function ($queryString) use ($keyword) {
            return $queryString->where('school_id', $keyword );
        })->orderByDesc('id')->paginate();
        return StudentResource::collection($models);
    }



    public function getPoliceStation($id){
        $police_station_id = PoliceStationHasSchool::query()->where('school_id',$id)
            ->with('school','policeStation')->get();
        return PoliceStationResource::collection($police_station_id);
    }

    public function enrollmentSituation(Request $request)
    {

        $keyword = $request->get('keyword', '');
        $query = School::query();
        $models = $query->when($keyword, function ($queryString) use ($keyword) {
            return $queryString->where('school_name', $keyword );
        })->paginate();
        return EnrollmentSituationResource::collection($models);
    }


    public function destroy($id)
    {
        $model = $this->findModel($id);
        event(new addLog(Auth::user(),'删除'.$model->name.'的报名信息'));
        Student::query()->where('entity_id',$model->entity_id)->delete();
        return response()->json(['state'=>'success']);

    }

    public function update(Request $request,$id)
    {
        \DB::beginTransaction();
        try {
            $children = $request->get('children');
            $name = $children[0]['name'];
            $sex = $children[0]['sex'];
            $nation = $children[0]['nation'];
            $police_station_id = $children[0]['police_station_id'];
            $id_code = $children[0]['id_code'];
            $relation = $children[0]['relation'];
            $prophylactic_code = $children[0]['prophylactic_code'];
            $father_name = $request->get('father_name');
            $father_works = $request->get('father_works');
            $father_id_code = $request->get('father_id_code');
            $father_phone_number = $request->get('father_phone_number');
            $mother_name = $request->get('mother_name');
            $mother_id_code = $request->get('mother_id_code');
            $mother_works = $request->get('mother_works');
            $mother_phone_number = $request->get('mother_phone_number');
            $family_address = $request->get('family_address');
            $section_id = $request->get('section_id');
            $father_type = $request->get('father_type');
            $mother_type = $request->get('mother_type');
            $twins = $request->get('twins');
            $is_twins = $request->get('is_twins');
            if($is_twins == true){
                $is_twins = 1;
            }else{
                $is_twins = 0;
            }


            $entity = $this->findEntity($id);
            $entity->father_name = $father_name;
            $entity->father_works = $father_works;
            $entity->father_id_code = $father_id_code;
            $entity->father_phone_number = $father_phone_number;
            $entity->mother_name = $mother_name;
            $entity->mother_id_code = $mother_id_code;
            $entity->mother_works = $mother_works;
            $entity->mother_phone_number = $mother_phone_number;
            $entity->family_address = $family_address;
            $entity->father_type = $father_type;
            $entity->mother_type = $mother_type;
            $entity->save();

            $model = $this->findModel($id);
            $exist = Student::query()->where('id_code',$id_code)
                ->where('id','!=',$model->id)->first();
            if($exist){
                return response()->json(['message'=>$name.'的身份证号已存在！'], 481);
            }
            $model->name = $name;
            $model->twins = $twins;
            $model->is_twins = $is_twins;
            $model->sex = $sex;
            $model->nation = $nation;
            $model->police_station_id = $police_station_id;
            $model->id_code = $id_code;
            $model->first_kid = 1;
            $model->entity_id = $entity->id;
            $model->relation = $relation;
            $model->prophylactic_code = $prophylactic_code;
            $model->section_id = $section_id;
            $model->save();

            Student::query()->where('entity_id',$model->entity_id)
                ->where('id','!=',$model->id)->delete();

            foreach ($children  as $index => $item){
                if ($index == 0){
                    continue;
                }
                $student = new Student();
                $student->name = $item['name'];
                $student->school_id = $model->school_id;
                $student->first_kid = 0;
                $student->sex = $item['sex'];
                $student->nation = $item['nation'];
                $student->police_station_id = $model->police_station_id;
                $student->twins = $twins;
                $student->id_code = $item['id_code'];
                $student->entity_id = $model->entity_id;
                $student->relation = $model->relation;
                $student->prophylactic_code = $item['prophylactic_code'];
                $student->section_id = $model->section_id;
                $student->is_twins = $twins;
                $student->first_kid = 0;
                $is_exist = Student::query()->where('id_code', $item['id_code'])
                    ->where('id','!=',$model->id)->first();
                if($is_exist){
                    return response()->json(['message'=>$item['name'].'的身份证号已存在！'], 481);

                }
                $student->save();
                event(new addLog(Auth::user(),'修改'.$item['name'].'的报名信息'));
            }
            event(new addLog(Auth::user(),'修改'.$name.'的报名信息'));
            \DB::commit();
            return response('添加成功');
        } catch (\Exception $e) {
            \DB::rollBack();
            return response()->json(['message'=>$e->getMessage()], 481);
        }
    }
    /**
     * @param $id
     * @return \Illuminate\Database\Eloquent\Model
     */
    public function findModel($id){
        $model = Student::query()->where('id',$id)->first();
        return $model;
    }

    /**
     * @param $id
     * @return \Illuminate\Database\Eloquent\Model
     */
    public function findEntity($id){
        $model = Student::query()->where('id',$id)->first();
        $entity = Entity::query()->where('id',$model->entity_id)->first();
        return $entity;
    }

    public function show($id){
        $model = Student::query()->where('id',$id)->first();
        $entity_id = $model->entity_id;
        $model = Student::query()->where('entity_id',$entity_id)->get();
        return new StudentUpdateResource($model);
    }

}
